package cn.flying.cloud.tool.code.entity;

import java.io.Serializable;

import cn.flying.cloud.base.common.annotation.Column;
import cn.flying.cloud.base.common.utils.lang.StringUtil;

/**
 * @author: admin
 * @date: 2024年07月01日 15:13
 * @version: 1.0
 */
public class Field implements Serializable {

    /**
     * 列名
     */
    @Column(value = "column_name")
    private String name;
    /**
     * 列类型
     */
    @Column(value = "column_type")
    private String type;
    /**
     * 主键
     */
    @Column(value = "column_key")
    private String key;
    /**
     * 注释
     */
    @Column(value = "column_comment")
    private String comment;
    /**
     * 属性名 列名转驼峰
     */
    private String propertyName;
    /**
     * 属性类型
     */
    private String propertyType;
    /**
     * 属性名首字母大写
     */
    private String capitalName;
    /**
     * jdbc类型
     */
    private String jdbcType;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public String getPropertyName() {
        return StringUtil.underlineToHump(this.name);
    }

    public void setPropertyName(String propertyName) {
        this.propertyName = propertyName;
    }

    public String getPropertyType() {
        if (this.type.startsWith("bigint")) {
            return "Long";
        } else if (this.type.startsWith("varchar") || this.type.startsWith("char") || this.type.startsWith("text") || this.type.startsWith("longtext")) {
            return "String";
        } else if (this.type.startsWith("date")) {
            return "Date";
        } else if (this.type.startsWith("int") || this.type.startsWith("tinyint") || this.type.startsWith("integer") || this.type.startsWith("mediumint")) {
            return "Integer";
        } else if (this.type.startsWith("double")) {
            return "Double";
        } else if (this.type.startsWith("float")) {
            return "Float";
        } else if (this.type.startsWith("decimal")) {
            return "BigDecimal";
        } else if (this.type.startsWith("blob")) {
            return "byte[]";
        } else {
            return "String";
        }
    }

    public void setPropertyType(String propertyType) {
        this.propertyType = propertyType;
    }

    public String getCapitalName() {
        return StringUtil.underlineToHeadUpper(this.name);
    }

    public void setCapitalName(String capitalName) {
        this.capitalName = capitalName;
    }

    public String getJdbcType() {
        if (this.type.startsWith("int") || this.type.startsWith("tinyint") || this.type.startsWith("integer") || this.type.startsWith("mediumint")) {
            return "INTEGER";
        } else if (this.type.startsWith("decimal") || this.type.startsWith("numeric")) {
            return "DECIMAL";
        } else if (this.type.startsWith("timestamp") || this.type.startsWith("datetime")) {
            return "TIMESTAMP";
        } else if (this.type.contains("(")) {
            return this.type.substring(0, this.type.indexOf("(")).toUpperCase();
        }
        return this.type.toUpperCase();
    }

    public void setJdbcType(String jdbcType) {
        this.jdbcType = jdbcType;
    }

    @Override
    public String toString() {
        return "Field{" +
                "name='" + name + '\'' +
                ", type='" + type + '\'' +
                ", key='" + key + '\'' +
                ", comment='" + comment + '\'' +
                ", propertyName='" + this.getPropertyName() + '\'' +
                ", propertyType='" + this.getPropertyType() + '\'' +
                ", capitalName='" + this.getCapitalName() + '\'' +
                '}';
    }
}
